<template>
  <div>
    <emptyTableNum
      ref="boxNum"
      :seeFlag="see"
      @setTerm="setTerm"
    ></emptyTableNum>
    <backInfoForm ref="backInfo" :seeFlag="see"></backInfoForm>
    <addPlanForm
      :showFCZYQ="true"
      ref="addPlan"
      :showUpload="false"
      :Tag="tag"
      @add="add"
      :seeFlag="see"
    ></addPlanForm>
    <!-- <newLeftMenu2 ref="newLeftMenu2" v-show="false"></newLeftMenu2> -->
  </div>
</template>

<script>
import emptyTableNum from "./module/emptyTableNum";
import addPlanForm from "../emptyBox/module/addPlanForm";
import backInfoForm from "./module/backInfoForm.vue";
// import newLeftMenu2 from '@/components/newLeftMenu2.vue'
export default {
  name: "heavyEmptyNum",
  components: { emptyTableNum, addPlanForm, backInfoForm, newLeftMenu2 },
  data() {
    return {
      tag: "",
      see: false,
    };
  },
  //通过路由规则，离开该组件时被调用
  beforeRouteLeave(to, from, next) {
    console.log("About--beforeRouteLeave", to, from);
    let nowJsonData = JSON.stringify(this.$refs.boxNum.form);
    let oldFormJson = JSON.stringify(this.$refs.boxNum.oriForm);
    // let nowJsonData1 = JSON.stringify(this.$refs.addPlan.form)
    // let oldFormJson1 = JSON.stringify(this.$refs.addPlan.oriForm)
    // console.log('================================ ',nowJsonData1,' 000000000000000000000000 ',oldFormJson1) //;
    if (nowJsonData != oldFormJson && to.query.name != "boxGoods") {
      // if(nowJsonData!=oldFormJson || nowJsonData1!=oldFormJson1){
      // if(this.tableData!= []){
      this.$nextTick(() => {
        this.$confirm("存在未提交信息，确认关闭？", {
          closeOnHashChange: false,
          closeOnClickModal: false,
          distinguishCancelAndClose: true,
          closeOnPressEscape: false,
          confirmButtonText: "确定",
          cancelButtonText: "取消",
        })
          .then((_) => {
            console.log("这边有问题", this.$route.path);
            // console.log('这边有问题',this.$route.current);
            this.$store.commit("setActiveMenuPath", next.fullPath);
            // this.$router.push(to.fullPath)
            next();
          })
          .catch((_) => {
            console.log(
              "是取消！！！！！！！！！！！！！！！！！",
              from.fullPath
            );
            this.$store.commit("setActiveMenuPath", from.fullPath);
            // this.$route.path = from.fullPath
            // this.$router.push(from.fullPath)
            // console.log('看看路由',this.$route.path)
            this.$nextTick(() => {
              this.$refs.newLeftMenu2.refresh =
                !this.$refs.newLeftMenu2.refresh;
              let path = {
                index: from.fullPath,
              };
              // this.$refs.newLeftMenu2.getActivePath(path)
              this.$refs.newLeftMenu2.getPath(from.fullPath);
              this.$refs.newLeftMenu2.linkTab(from.fullPath);
            });
            // this.$refs.newLeftMenu2.linkTab(from.fullPath)
            // console.log('path: ' + from.fullPath,this.$route.path);
          });
      });
    } else {
      next();
    }
  },
  methods: {
    setTerm(val) {
      console.log(val);
      this.$refs.addPlan.form.termCode = val;
      this.$refs.addPlan.$forceUpdate();
    },
    getInfo() {
      this.$nextTick(() => {
        if (undefined !== this.$route.query.id) {
          if ("see" === this.$route.query.state) {
            this.see = true;
          } else {
            this.see = false;
          }
          this.$api
            .contentSelectAll({
              id: this.$route.query.id,
            })
            .then((res) => {
              let data = res.data.data[0];
              let name = this.$refs.addPlan.form.cstCoperName;
              let plan = this.$refs.addPlan.form.planTagName;
              this.$refs.addPlan.form = JSON.parse(JSON.stringify(data));
              this.$refs.addPlan.form.cstCoperName = name;
              this.$refs.addPlan.form.planTagName = plan;

              this.$refs.addPlan.$refs.myUpload.file = JSON.parse(
                JSON.stringify(data.planAttachmentList)
              );
              //console.log(this.$refs.addPlan.form)
              //this.$refs.boxNum.form=JSON.parse(JSON.stringify(data.vanUseRequirements))
              this.$refs.backInfo.form = JSON.parse(
                JSON.stringify(data)
              ).planReturn;
              this.$refs.backInfo.form.termCode = JSON.parse(
                JSON.stringify(data)
              ).termCode;
              this.$refs.boxNum.tableData = JSON.parse(
                JSON.stringify(data)
              ).planContainers;
              //this.$refs.boxNum.form.termCode=JSON.parse(JSON.stringify(data)).termCode
            })
            .catch();
        }
      });
    },
    link() {
      this.$router.push({
        query: {
          name: "boxGoods",
        },
        name: "success",
      });
    },
    checkPlan() {
      let result = false;
      this.$refs.addPlan.$refs.form.validate((valid) => {
        result = valid;
      });
      return result;
    },
    add() {
      this.$refs.addPlan.loading = true;
      let that = this;
      let formPlan = this.checkPlan();
      if (!formPlan) {
        this.$refs.addPlan.loading = false;
        return;
      }
      let select = this.$refs.boxNum.selection;
      if (0 === select.length) {
        this.$refs.addPlan.loading = false;
        this.$message.error("请先选择箱信息！");
        return;
      }
      let form = this.$refs.addPlan.form;
      form.planReturn = this.$refs.backInfo.form;
      form.termCode = form.planReturn.termCode;
      form.planContainers = select;
      form.tradeFlag = select[0].internalfg;
      if ("Y" === select[0].internalfg) {
        form.planReturn.stsCStatusCd = "AF";
      } else {
        form.planReturn.stsCStatusCd = "OF";
      }
      form.returnTermcd = form.termCode;
      let par = "";
      if (undefined === form.id || null === form.id) {
        par = "TKFZ_Appoint_Add";
      } else {
        par = "TKFZ_Appoint_Edit";
      }
      this.$api
        .shipE(form, par)
        .then((res) => {
          if (200 === res.code) {
            this.$message.success({
              message: undefined !== form.id ? "编辑成功!" : "新增成功!",
              onClose() {
                that.link();
              },
            });
          } else {
            this.$refs.addPlan.loading = false;
            //this.$message.error(res.message)
          }
        })
        .catch((res) => {
          this.$refs.addPlan.loading = false;
          //this.$message.error(res.message)
        });
    },
  },

  computed: {},

  mounted() {
    this.tag = "PERF_WC";
    this.getInfo();
  },
};
</script>
<style lang="scss" scoped>
:deep .el-input.is-disabled .el-input__inner {
  color: #666;
}
:deep .el-textarea.is-disabled .el-textarea__inner {
  color: #666;
}
</style>
